//
//  UIColorExtension.swift
//  CocoProject
//
//  Created by Ruthless on 2023/8/17.
//

import UIKit

public enum GradientDirection: Int {
    case Horizontal
    case Vertical
}

extension UIColor {
    static func rgba(_ r: CGFloat, _ g: CGFloat, _ b: CGFloat, _ a: CGFloat = 1) -> UIColor {
        return UIColor.init(red: r/255, green: g/255, blue: b/255, alpha: a)
    }
    
    func toImage() -> UIImage? {
        let r = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1)
        UIGraphicsBeginImageContext(r.size)
        let context = UIGraphicsGetCurrentContext()
        context?.setFillColor(self.cgColor)
        context?.fill(r)
        let img: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return img
    }
    
    class func colorWithHex(_ hexStr:String?, _ alpha:Float = 1.0) -> UIColor{
        if hexStr == nil{
            return UIColor.clear
        }
        var cStr = hexStr!.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased() as NSString;
        
        if(cStr.length < 6){
            return UIColor.clear;
        }
        
        if(cStr.hasPrefix("0X")) {
            cStr = cStr.substring(from: 2) as NSString
        }
        
        if(cStr.hasPrefix("#")){
            cStr = cStr.substring(from: 1) as NSString
        }
        
        if(cStr.length != 6){
            return UIColor.clear;
        }
        
        let rStr = (cStr as NSString).substring(to: 2)
        let gStr = ((cStr as NSString).substring(from: 2) as NSString).substring(to: 2)
        let bStr = ((cStr as NSString).substring(from: 4) as NSString).substring(to: 2)
        
        var r : UInt64 = 0x0
        var g : UInt64 = 0x0
        var b : UInt64 = 0x0
        Scanner.init(string: rStr).scanHexInt64(&r);
        Scanner.init(string: gStr).scanHexInt64(&g);
        Scanner.init(string: bStr).scanHexInt64(&b);
        
        return UIColor.init(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: CGFloat(alpha));
        
    }
    
    static func gradientFromColor(colors: [CGColor], direction:GradientDirection, range: CGFloat) -> UIColor{
        if direction == .Horizontal{
            let size = CGSize(width: range, height: 1)
            UIGraphicsBeginImageContextWithOptions(size, false, 0)
            let context = UIGraphicsGetCurrentContext()
            let colorspace = CGColorSpaceCreateDeviceRGB()
            let gradient = CGGradient(colorsSpace: colorspace, colors: colors as CFArray, locations: nil)!
            context?.drawLinearGradient(gradient, start: CGPoint(x: 0, y: 0), end: CGPoint(x: size.width, y: 0), options: CGGradientDrawingOptions(arrayLiteral: .drawsBeforeStartLocation,.drawsAfterEndLocation))
            let gradientImage:UIImage = UIGraphicsGetImageFromCurrentImageContext() ?? UIImage()
            UIGraphicsEndImageContext()
            return UIColor(patternImage: gradientImage)
        }else if direction == .Vertical{
            let size = CGSize(width: range, height: 1)
            UIGraphicsBeginImageContextWithOptions(size, false, 0)
            let context = UIGraphicsGetCurrentContext()
            let colorspace = CGColorSpaceCreateDeviceRGB()
            let gradient = CGGradient(colorsSpace: colorspace, colors: colors as CFArray, locations: nil)!
            context?.drawLinearGradient(gradient, start: CGPoint(x: 0, y: 0), end: CGPoint(x: 0, y: size.height), options: CGGradientDrawingOptions(arrayLiteral: .drawsBeforeStartLocation,.drawsAfterEndLocation))
            let gradientImage:UIImage = UIGraphicsGetImageFromCurrentImageContext() ?? UIImage()
            UIGraphicsEndImageContext()
            return UIColor(patternImage: gradientImage)
        }else{
            return .black
        }
    }
}
